// pages/login/forget_password/forget_password.js
var App = getApp();

var ApiUrl = App.data.ApiUrl;

var Common = require('../../../utils/common.js');


Page({
    data: {
        userName: '',
        SMCTime: 60,
        SMCode_Lock: false,
        SMCode_Msg: '获取验证码',
        sign: '',
        password: '',
        passwords: '',
        signsd: false,
        SMCode: '',
        token: ''
    },
    onLoad: function (options) {
        // 页面初始化 options为页面跳转所带来的参数
        wx.setNavigationBarTitle({
            title: '找回密码'
        }) 
        var signs = options.sign ? options.sign : '';
        var userName = options.userName ? options.userName : '';
        this.setData({
            sign: signs,
            userName: userName,
        })

        // 切换顾问 --start
        Common.showContips(this);
        // this.animation = Common.showConsulantBar();
        // 切换顾问 --end
    },
    onPullDownRefresh: function () {
        wx.stopPullDownRefresh();
    },

    // 顾问切换提示条
    show_bar: function () {
        Common.show_bar(this);
    },
    // 顾问信息展示
    showCard: function () {
        Common.showCard(this);
    },
    // 滑动起始点
    my_start: function (e) {
        Common.my_start(e, this);
    },
    // 滑动结束点
    my_end: function (e) {
        Common.my_end(e, this);
    },
    // 切换顾问
    changeConsulant: function (e) {
        Common.changeConsulant(e);
    },
    getSMCode: function (e) {
        console.log('SMCode');
        var page = this;
        var userName = this.data.userName;
      var phonetest = /^0?1\d{10}$/;

        page.setData({
            SMCode_Lock: true
        });
        if (!userName) {
            Common.Alert('登录提示', '请输入手机号码');
            return false;
        } else if (!phonetest.test(userName)) {
            Common.Alert('登录提示', '请输入正确的手机号码');
            return false;
        }
        if (page.data.SMCTime == 60) {
            // 获取验证码
            wx.request({
                url: ApiUrl + '/AppMaster/Login/backPwdSendMessageCode?phone=' + userName,
                header: {
                    'content-type': 'json'
                },
                success: function (res) {
                    console.log(res.data);
                    if (res.data.result == 0) {
                        wx.showToast({
                            title: '验证码已发送',
                            icon: 'success',
                            duration: 2000
                        })
                        // 倒计时显示
                        page.SMCode_Msg();
                    } else {
                        page.setData({
                            SMCTime: 60,
                            SMCode_Lock: false,
                            SMCode_Msg: '获取验证码'
                        })
                        Common.Alert('错误提示', res.data.msg);
                    }
                }
            })
        }
    },
    updateUserName: function (e) {
        this.setData({
            userName: e.detail.value,
        })
        if (this.data.SMCTime < 60) {
            this.setData({
                SMCTime: 0,
            })
        }
    },
    updateSMCode: function (e) {
        this.setData({
            SMCode: e.detail.value,
        })
    },
    updatePassword: function (e) {
        this.setData({
            password: e.detail.value,
        })
    },
    updatePasswords: function (e) {
        this.setData({
            passwords: e.detail.value,
        })
    },
    SMCode_Msg: function () {
        console.log('SMCode_Msg');
        var page = this;
        var SMCTimers;
        var SMCTime = page.data.SMCTime;
        var SMCode_Msg = page.data.SMCode_Msg;
        var SMCode_Lock = page.data.SMCode_Lock;
        if (SMCTime > 1) {
            SMCTime--;
            SMCode_Msg = '重新获取(' + SMCTime + ')';
            page.setData({
                SMCTime: SMCTime,
                SMCode_Msg: SMCode_Msg
            })
            // 定时器执行
            SMCTimers = setTimeout(function () {
                page.SMCode_Msg();
            }, 1000);

        } else {
            clearTimeout(SMCTimers);
            page.setData({
                SMCTime: 60,
                SMCode_Lock: false,
                SMCode_Msg: '获取验证码'
            })
        }
    },
    onForgetPasswordSMC: function () {
        console.log('onForgetPasswordSMC');
        var page = this;
        var userName = this.data.userName;
        var SMCode = this.data.SMCode;
      var phonetest = /^0?1\d{10}$/;
        if (!userName) {
            Common.Alert('验证提示', '请输入手机号码');
            return false;
        } else if (!phonetest.test(userName)) {
            Common.Alert('验证提示', '请输入正确的手机号码');
            return false;
        }
        if (!SMCode) {
            Common.Alert('验证提示', '请输入验证码');
            return false;
        }
        wx.showLoading({
            title: '正在验证中...',
        })
        wx.request({
            url: ApiUrl + '/AppMaster/Login/checkPwdMessageCode?phone=' + userName + '&code=' + SMCode,
            header: {
                'content-type': 'json'
            },
            success: function (res) {
                console.log(res.data);
                if (res.data.result == 0) {
                    // 关闭加载
                    wx.hideLoading();
                    wx.showToast({
                        title: '验证成功',
                        icon: 'success',
                        duration: 2000
                    })
                    page.setData({
                        sign: res.data.data.sign,
                    })
                } else {
                    // 关闭加载
                    wx.hideLoading();
                    page.setData({
                        SMCTime: 0, 
                    })
                    Common.Alert('验证提示', res.data.msg);
                }
            }
        })
    },
    ResetPassword: function () {
        var page = this;
        var userName = page.data.userName;
        var password = page.data.password;
        var sign = page.data.sign;
        var confirmPassword = page.data.passwords;
      var phonetest = /^0?1\d{10}$/;
        var passwordtest = /^.{6,16}$/
        if (page.data.signsd) {
            return false;
        }
        if (!userName) {
            Common.Alert('验证提示', '请输入手机号码');
            return false;
        } else if (!phonetest.test(userName)) {
            Common.Alert('验证提示', '请输入正确的手机号码');
            return false;
        }
        if (!password) {
            Common.Alert('修改密码', '请输入密码');
            return false;
        } if (!passwordtest.test(password)) {
            Common.Alert('修改密码', '密码长度必须在6-16个字符之间！');
            page.setData({
                password: '',
                confirmPassword: '',
            })
            return false;
        } else if (!confirmPassword) {
            Common.Alert('修改密码', '请输入确认密码');
            return false;
        } else if (password != confirmPassword) {
            Common.Alert('修改密码', '密码不一致！');
            return false;
        }
        wx.showLoading({
            title: '正在修改中...',
        })
        wx.request({
            url: ApiUrl + '/AppMaster/Login/resetPwdCode',
            data: {
                phone: userName,
                sign: sign,
                password: password,
                confirmPassword: confirmPassword
            },
            header: {
                'content-type': 'json'
            },
            success: function (res) {
                if (res.data.result == 0) {
                    // 关闭加载
                    wx.hideLoading();
                    wx.showToast({
                        title: '密码修改成功！',
                        icon: 'success',
                        duration: 2000,
                        signsd: !page.data.signsd
                    })
                    wx.navigateBack()
                } else {
                    // 关闭加载
                    wx.hideLoading();
                    page.setData({
                        SMCTime: 0,
                    })
                    Common.Alert('验证提示', res.data.msg);
                }
            }
        })
    },
    onPullDownRefresh: function () {
        wx.stopPullDownRefresh()
    }
})